package hp.com.servlet.UsersServlet;

import hp.com.entity.Users;
import hp.com.service.UsersService;
import hp.com.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    private UsersService usersService = new UsersServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            req.setCharacterEncoding("utf-8");
            String name = req.getParameter("name");
            String pwd = req.getParameter("pwd");
            Users users = new Users(name,pwd);
            //执行登陆操作
            boolean b = usersService.UsersById(users);
            if (b) {
                //如果登陆成功
                //现将用户对象写入session
                users=usersService.checkPhone(users);
                HttpSession session = req.getSession();
                session.setAttribute("userSession",users);
                resp.getWriter().print(true);
            }else {
                resp.getWriter().print(false);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
}
